﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Common.DTO;
using Database;
using GameAPI.Models.GameObject;
using GameLogic.Place;

namespace GameAPI.Controllers
{
    public class TileController : ApiController
    {
        private TileManager _objectManager;

        public TileController()
        {
            _objectManager = new TileManager();
        }

        // OL: Hämta Tiles sker genom PlaceController (eftersom man måste ange vilket place id)
        //[HttpGet]
        //public HttpResponseMessage GetAll(GameObjectChangeRequestModel model)
        //{
        //    return Request.CreateResponse(HttpStatusCode.OK, _objectManager.GetTiles());
        //}

        [HttpPost]
        public HttpResponseMessage Change(GameObjectChangeRequestModel model)
        {
            if (model == null || String.IsNullOrEmpty(model.Amount.ToString(CultureInfo.InvariantCulture)) ||
                String.IsNullOrEmpty(model.Id.ToString()) ||
                String.IsNullOrEmpty(model.X.ToString(CultureInfo.InvariantCulture)) ||
                String.IsNullOrEmpty(model.Y.ToString(CultureInfo.InvariantCulture)))
            {
                Request.CreateResponse(HttpStatusCode.BadRequest, "Request model is invalid. No NULL values");
            }

            var tileDTO = new TileDTO()
            {
                Id = model.Id,
                //Amount = model.Amount,
                X = model.X,
                Y = model.Y
            };
            var result = _objectManager.ChangeGameObject(tileDTO);


            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [HttpPost]
        public HttpResponseMessage Move(GameObjectChangeRequestModel model)
        {
            if (model == null || String.IsNullOrEmpty(model.Id.ToString()) ||
                String.IsNullOrEmpty(model.X.ToString(CultureInfo.InvariantCulture)) ||
                String.IsNullOrEmpty(model.Y.ToString(CultureInfo.InvariantCulture)))
            {
                Request.CreateResponse(HttpStatusCode.BadRequest, "Request model is invalid. No NULL values");
            }

            var gameObject = new TileDTO()
            {
                Id = model.Id,
                X = model.X,
                Y = model.Y
            };
            var result = _objectManager.MoveGameObject(gameObject);

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }
}